package com.songlanyun.modules.config.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.config.dao.SystemSaleBaseConfigDao;
import com.songlanyun.modules.config.entity.SystemSaleBaseConfigEntity;
import com.songlanyun.modules.config.service.SystemSaleBaseConfigService;
import com.songlanyun.modules.exception.ConfigException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("yxtSystemSaleBaseConfigService")
public class SystemSaleBaseConfigServiceImpl extends ServiceImpl<SystemSaleBaseConfigDao, SystemSaleBaseConfigEntity> implements SystemSaleBaseConfigService {

    /**
     * 列表
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String expressCustomer = MapUtil.getStr(params, "express_customer");//快递100 Customer
        String expressKey = MapUtil.getStr(params, "express_key");//快递100 Key
        String smsAccessKeyId = MapUtil.getStr(params, "smsAccess_key_d");//短信通知（阿里云短信）AccessKeyId
        String smsAccessKeySecret = MapUtil.getStr(params, "smsAccess_key_secret");//短信通知（阿里云短信）AccessKeySecret

        IPage<SystemSaleBaseConfigEntity> page = this.page(
                new Query<SystemSaleBaseConfigEntity>().getPage(params),
                new LambdaQueryWrapper<SystemSaleBaseConfigEntity>()
                        .eq(StringUtils.isNotBlank(expressCustomer), SystemSaleBaseConfigEntity::getExpressCustomer, expressCustomer)
                        .eq(StringUtils.isNotBlank(expressKey), SystemSaleBaseConfigEntity::getExpressKey, expressKey)
                        .eq(StringUtils.isNotBlank(smsAccessKeyId), SystemSaleBaseConfigEntity::getSmsAccessKeyId, smsAccessKeyId)
                        .eq(StringUtils.isNotBlank(smsAccessKeySecret), SystemSaleBaseConfigEntity::getSmsAccessKeySecret, smsAccessKeySecret)
        );

        return new PageUtils(page);
    }

    /**
     * 是否报错
     * @param id
     * @param isThrow
     * @return
     */
    @Override
    public SystemSaleBaseConfigEntity getById(Integer id, boolean isThrow) {
        SystemSaleBaseConfigEntity entity = this.getById(id);
        if (entity == null) {
            if (isThrow) {
                throw new RRException(ConfigException.CONFIG_NOT_EXIST);
            }
        }
        return entity;

    }

}
